package com.go.where.manager.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.go.where.manager.common.Constants;
import com.go.where.manager.dao.TopicDao;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.page.BaseQuery.OrderType;
import com.go.where.manager.pojo.TopicInfo;
import com.go.where.manager.pojo.user.UserInfoVO;
import com.go.where.manager.service.TopicService;

@Service("topicService")
public class TopicServiceImpl implements TopicService {

	@Resource(name = "topicDao")
	private TopicDao topicDao;

	@Override
	public TopicInfo queryByPrimartKey(String cid) {
		return topicDao.getByKey(cid);
	}

	@Override
	public Pagination<TopicInfo> queryPage(TopicInfo topicInfo) {
		if (StringUtils.isNotEmpty(topicInfo.getOrderBy())
				&& topicInfo.getOrderType() != null) {
			if (topicInfo.getOrderType() == OrderType.asc) {
				topicInfo.setPage_orderByAsc(topicInfo.getOrderBy());

			} else {
				topicInfo.setPage_orderByDesc(topicInfo.getOrderBy());
			}
		}
		Integer count = topicDao.totalCount(topicInfo);
		topicInfo.calculate();
		List<TopicInfo> list = topicDao.queryList(topicInfo);
		Pagination<TopicInfo> page = topicDao.queryPagination(count, list,
				topicInfo);
		return page;
	}

	@Override
	public int save(TopicInfo topicInfo, UserInfoVO currentUser) {
		topicInfo.setPublishTime(new Date());
		topicInfo.setUserId(currentUser.getUserId());
		topicInfo.setIsDelete(Constants.NOT_DELETED);
		return topicDao.insert(topicInfo);
	}

	@Override
	public int update(TopicInfo topicInfo, UserInfoVO currentUser) {
		topicInfo.setUpdateTime(new Date());
		topicInfo.setUserId(currentUser.getUserId());
		return topicDao.update(topicInfo);
	}

	@Override
	public List<TopicInfo> queryAll() {
		return topicDao.queryAll();
	}

}
